package android.mystyle.phonebook;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Act_PhonebookList extends ListActivity
{
	private ListView mContactList;
	
//	private static String[] STR_PB_PROJECTION = new String[]{Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
//	private static String[] STR_PB_PROJECTION = new String[]{Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pb_list);
		
		//mContactList = (ListView) this.findViewById(R.id.PB_ListView);
		mContactList = (ListView) this.findViewById(android.R.id.list);		

		//Button button1 = (Button) this.findViewById(R.id.button_test);
		//button1.setBackgroundResource(android.R.drawable.btn_star);
        //Cursor c = getContentResolver().query(Phone.CONTENT_URI, STR_PB_PROJECTION, null, null, null);
        //startManagingCursor(c);
        
//        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, STR_PB_PROJECTION));
		initPhonebookList();
	}

    private void initPhonebookList() 
    {
        // Build adapter with contact entries
        Cursor cursor = getPhonebookList();
        String[] listItems = new String[] {ContactsContract.Data.DISPLAY_NAME};
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.pb_list_item, cursor,
        		listItems, new int[] {R.id.PB_List_Item});
        mContactList.setAdapter(adapter);
    }
    
    private Cursor getPhonebookList()
    {
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};

//String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        //return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        return managedQuery(uri, projection, null, selectionArgs, sortOrder);
    }	
}
